const app = getApp()
const mockData = require('../../utils/data.js')

Page({
  data: {
    favorites: []
  },
  
  onLoad() {
    this.loadFavorites()
  },
  
  onShow() {
    this.loadFavorites()
  },
  
  loadFavorites() {
    const favorites = app.globalData.favorites || []
    
    // 合并产品信息
    const favoritesWithInfo = favorites.map(fav => {
      const product = mockData.products.find(p => p.id === fav.id) || mockData.products[0]
      return {
        ...fav,
        ...product
      }
    })
    
    this.setData({
      favorites: favoritesWithInfo
    })
  },
  
  goToProductDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/product-detail/product-detail?id=${id}`
    })
  },
  
  removeFavorite(e) {
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确定要取消收藏吗？',
      success: (res) => {
        if (res.confirm) {
          app.removeFromFavorites(id)
          this.loadFavorites()
          wx.showToast({
            title: '已取消收藏',
            icon: 'success'
          })
        }
      }
    })
  }
})


